package hot_100;

import com.sun.source.tree.Tree;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author: Darven
 * @createTime: 2025-10-03  20:23
 * @description: TODO
 */
public class P101 {

    // 递归
//    public boolean isSymmetric(TreeNode root) {
//       return check(root.left, root.right);
//    }
//
//    private boolean check(TreeNode left, TreeNode right){
//        if(left==null&&right==null) return true;
//        if(left==null||right==null) return false;
//        return left.val== right.val && check(left.right, right.left) && check(left.left, right.right);
//    }

    // 递归
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true; // 空树是对称的
        Queue<TreeNode> queue = new ArrayDeque<>();

        queue.offer(root.left);
        queue.offer(root.right);

        while (!queue.isEmpty()) {
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();

            if (left == null && right == null) {
                continue;
            }

            if (left == null || right == null || left.val != right.val) {
                return false;
            }
            queue.offer(left.left);
            queue.offer(right.right);


            queue.offer(left.right);
            queue.offer(right.left);


        }

        return true;
    }
}
